import {Field} from "../../field/field.model";
import {extend} from "../../../../support/extend";
import {generateFieldId} from "../../../../support/generateFieldId";

enum CellType {
  explain = "cellExplain",
  separator = "cellSeparator",
  data = "data",
}

class Cell {
  type: CellType = CellType.data;
  field: Field = new Field();
  isEdit: boolean = false;
  isNew: boolean = true;

  constructor(type?: string) {
    this.field.type = type;
  }

  static generateNew(cellType: CellType, field?: Field): Cell {
    let cell = new Cell();
    cell.type = cellType;
    // 数据列
    if (cellType == CellType.data) {
      // 新增
      if (!field) {
        cell.isNew = true;
        cell.field.type = "text";
        cell.field.fieldId = generateFieldId();
      } else {
        cell.field = field;
      }
    }
    // 其他列
    else {
      cell.field.type = cell.field.fieldId = cellType;
    }
    return cell
  }

  static copy(oldCell: Cell): Cell {
    let newCell = Cell.generateNew(oldCell.type, extend({}, oldCell.field))
    newCell.field.fieldId = generateFieldId();
    newCell.field.name = newCell.field.name + " 副本";
    return newCell;
  }
}


export {CellType, Cell}
